package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.ModifyInfoParams;
import com.huizhonglm.diap.api.model.user.UploadIconResponse;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.upload.Upload;
import com.huizhonglm.diap.core.upload.UploadCallback;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.user.iView.UserInfoIView;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-21 09:33
 */
public class UserInfoPresenter {

    private UserInfoIView iView;

    public UserInfoPresenter(UserInfoIView iView) {
        this.iView = iView;
    }

    public void modifyInfo(final String flag, final String... newValue){
        String apiName = "modifyInfo";
        ModifyInfoParams params = null;
        switch (flag) {
            case Constants.MODIFY_NAME:
                params = new ModifyInfoParams(API.buildUri(API.MODIFY_NAME));
                apiName = "modifyName";
                params.name = newValue[0];
                break;
            case Constants.MODIFY_NICKNAME:
                params = new ModifyInfoParams(API.buildUri(API.MODIFY_NICKNAME));
                apiName = "modifyNickname";
                params.nickname = newValue[0];
                break;
            case Constants.MODIFY_SEX:
                params = new ModifyInfoParams(API.buildUri(API.MODIFY_SEX));
                apiName = "modifySex";
                params.sex = newValue[0];
                break;
            case Constants.MODIFY_DISTRICT:
                params = new ModifyInfoParams(API.buildUri(API.MODIFY_DISTRICT));
                apiName = "modifyDistrict";
                params.provinceId = newValue[0];
                params.cityId = newValue[1];
                params.districtId = newValue[2];
                break;
            case Constants.MODIFY_ADDRESS:
                params = new ModifyInfoParams(API.buildUri(API.MODIFY_ADDRESS));
                apiName = "modifyAddress";
                params.address = newValue[0];
                break;
        }
        if (params != null) {
            params.userId = UserUtil.getUserId();
            API.http().post(apiName, params, new HttpCallback() {
                @Override
                public void onSuccess(String result) {
                    if (!EmptyUtil.isEmpty(result)) {
                        CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                        if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                            if (flag.equals(Constants.MODIFY_DISTRICT)){
                                iView.onModifySuccess(flag, newValue[2]);
                            } else {
                                iView.onModifySuccess(flag, newValue[0]);
                            }
                        } else {
                            iView.onModifyFailure(flag);
                        }
                    } else {
                        iView.onModifyFailure(flag);
                    }
                }

                @Override
                public void onFailure(Throwable ex) {
                    iView.onModifyFailure(flag);
                }
            });
        } else {
            iView.onModifyFailure(flag);
        }
    }

    public void uploadIcon(String iconPath) {
        String url = API.buildUri(API.UPLOAD_FILE);
        String fileKey = "userIconFile";
        Map<String, String> params = new HashMap<>();
        params.put("userId", UserUtil.getUserId());
        params.put("inputFileName", fileKey);
        Upload.getInstance().toUpload(iconPath, fileKey, url, params, new UploadCallback() {
            @Override
            public void onUploadSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    UploadIconResponse resp = JSON.parseObject(result, UploadIconResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())) {
                        iView.onUploadSuccess(resp.getHeaderImg());
                    } else {
                        iView.onUploadFailure();
                    }
                } else {
                    iView.onUploadFailure();
                }
            }

            @Override
            public void onUploadFailure(String errorMsg) {
                iView.onUploadFailure();
            }
        });
    }

}
